package com.archstoday.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.common.SpfUtils;
import com.archstoday.view.AutoNewLineLayout;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页面
 */
public class SearchActivity extends AsScrollBaseActivity {

    private EditText mSearchEdit;
    private AutoNewLineLayout mHistoryLayout;

    private List<String> mSearchHistory;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_search);
        mSearchEdit = (EditText) findViewById(R.id.search_edit);
        mHistoryLayout = (AutoNewLineLayout) findViewById(R.id.search_history_layout);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSearchHistory = MySharedPreferencesMgr.getList(SpfUtils.SEARCH_HISTORY);
        if (mSearchHistory != null) {
            showHistory();
        }
    }

    private void showHistory() {
        mHistoryLayout.removeAllViews();
        for (int i = 0; i < mSearchHistory.size(); i++) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_screen, null);
            TextView history = (TextView) view.findViewById(R.id.item_tv);

            history.setText(mSearchHistory.get(i));
            history.setTag(mSearchHistory.get(i));
            history.setBackgroundResource(R.drawable.details_btn);
            history.setOnClickListener(click);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.rightMargin = (int) getResources().getDimension(R.dimen.dp10);
            mHistoryLayout.addView(view, params);
        }
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.search_cancel_btn);
        setOnClickListener(R.id.search_clear_btn);

        mSearchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    String str = textView.getText().toString();
                    if (!StringUtil.isEmpty(str)) {
                        if (mSearchHistory != null) {
                            mSearchHistory.add(str);
                        } else {
                            mSearchHistory = new ArrayList<>();
                            mSearchHistory.add(str);
                        }
                        if (mSearchHistory.size() > 20) {
                            mSearchHistory.remove(0);
                        }
                        MySharedPreferencesMgr.setList(SpfUtils.SEARCH_HISTORY, mSearchHistory);

                        Intent intent = new Intent(mContext, SearchDetailsActivity.class);
                        intent.putExtra("search", str);
                        startActivity(intent);
                        appFinish();
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void treatClickEvent(View view) {
        switch (view.getId()) {
            case R.id.search_cancel_btn:
                getEvent(BaseApp.E_CANCEL_SEARCH, BaseApp.PAGE_TYPE_CANCEL_SEARCH);
                appFinish();
                break;
            case R.id.search_clear_btn:
                mSearchHistory = new ArrayList<>();
                MySharedPreferencesMgr.setList(SpfUtils.SEARCH_HISTORY, mSearchHistory);
                mHistoryLayout.removeAllViews();
                break;
            case R.id.item_tv:
                Intent intent = new Intent(mContext, SearchDetailsActivity.class);
                intent.putExtra("search", (String) view.getTag());
                startActivity(intent);
                appFinish();
                break;
        }
    }
}